package com.easycms.constant;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.easycms.framework.configs.JacksonSerializerModifier;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.TimeZone;

/**
 * @author yushuo
 * @className
 * @descripton TODO
 * @date 2021/2/5 13:47
 **/
public class JacksonConstants {

    /**
     * @author yushuo
     * @description //jackson ObjectMapper
     * @date 13:52 2021/2/5
     * @param
     * @return
    **/
    public static ObjectMapper objectMapper;
    /**
     * @author yushuo
     * @description //jackson MappingJackson2HttpMessageConverter
     * @date 13:52 2021/2/5
     * @param
     * @return
     **/
    public static  MappingJackson2HttpMessageConverter jackson2HttpMessageConverter;
    /**
     * @author yushuo
     * @description //jackson MappingJackson2JsonView
     * @date 13:52 2021/2/5
     * @param
     * @return
     **/
    public static MappingJackson2JsonView mappingJackson2JsonView;
    static {
        objectMapper=objectMapper();
        jackson2HttpMessageConverter=jackson2HttpMessageConverter();
        mappingJackson2JsonView=mappingJackson2JsonView();
    }

    /**
    *
     * @author yushuo
     * @description //jackson配置jackson2HttpMessageConverter
     * @date 13:49 2021/2/5
     * @param []
     * @return org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
    **/
    private static MappingJackson2HttpMessageConverter jackson2HttpMessageConverter(){

        return new MappingJackson2HttpMessageConverter(objectMapper());
    }

    /**
    *
     * @author yushuo
     * @description //jackson配置 objectMapper
     * @date 13:51 2021/2/5
     * @param []
     * @return com.fasterxml.jackson.databind.ObjectMapper
    **/
    private static ObjectMapper objectMapper(){
        final Jackson2ObjectMapperBuilder builder=new Jackson2ObjectMapperBuilder();
        final ObjectMapper objectMapper=builder.build();
        SimpleModule simpleModule=new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE,ToStringSerializer.instance);

        objectMapper.registerModule(simpleModule);
        objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER,true);

        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));

        //null值处理
        SerializerFactory serializerFactory= objectMapper.getSerializerFactory();
        serializerFactory.withSerializerModifier(new JacksonSerializerModifier());
        objectMapper.setSerializerFactory(serializerFactory);
        return objectMapper;
    }
    /**
     *
     * @author yushuo
     * @description //jackson配置
     * @date 13:49 2021/2/5
     * @param []
     * @return org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
     **/
    private static MappingJackson2JsonView mappingJackson2JsonView(){
        return new MappingJackson2JsonView(objectMapper());
    }





}
